package cn.yinuo.website.user.web;


import cn.yinuo.website.common.bean.BaseQueryVo;
import cn.yinuo.website.common.config.JsonParam;
import cn.yinuo.website.common.util.PageQueryUtils;
import cn.yinuo.website.user.entity.User;
import cn.yinuo.website.user.service.IUserService;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shiyang
 * @since 2019-05-22
 */
@RestController
@RequestMapping("/api/users")
@Api(description="用户")
public class UserController {
    @Autowired
    private IUserService userService;
    @ApiOperation(value = "新增" ,  notes="新增")
    @PostMapping
    public User createUser(@RequestBody User user){
        System.out.println("createUser:::"+user.toString());
        return userService.add(user);
    }

    @ApiOperation(value = "修改" ,  notes="修改")
    @PutMapping
    public User updateUser(@RequestBody User user){
        System.out.println("updateUser:::"+user.toString());
        return userService.modify(user);
    }

    @ApiOperation(value = "删除" ,  notes="删除")
    @DeleteMapping("{id}")
    public void deleteUser(@PathVariable("id") String userId){
        System.out.println("deleteUser:::"+userId);
        userService.delete(userId);
    }

    @ApiOperation(value = "根据id查询" ,  notes="根据id查询")
    @GetMapping("{id}")
    public User queryUser(@PathVariable("id") String userId){
        System.out.println("queryUser:::"+userId);

        return userService.queryById(userId);
    }

    @GetMapping()
    @ApiOperation("分页查询")
    @ApiImplicitParam(name = "queryVo",dataType = "BaseQueryVo" ,paramType = "query")
    public Page<User> page(@JsonParam BaseQueryVo<User> queryVo){
        return userService.selectPage(queryVo.getPageObj(),PageQueryUtils.getWrapper(queryVo));
    }
}
